// DateTime.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include "Windows.h"
#include <ctype.h>
#include "string.h"
#include "stdafx.h"
#include "Windows.h"
#include <ctype.h>
#include "string.h"
#include "dateTime.h"


#define startOfMonStr 8

char * dt = "Tue, 14 Apr 2015 11:46:17 GMT";

struct tm SensorTime;





//int _tmain(int argc, _TCHAR* argv[])
int testTime()
{
	long t;
	bool res = ISAzMSDateTime(dt);
	if (res)
		t = (long)ParseDateTime(dt, &SensorTime);
	return 0;
}

const char *  months[12] = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
const char *  days[7] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };

bool ISAzMSDateTime(char * dt)
{
	//char * dt = "Tue, 14 Apr 2015 11:46:17 GMT";
	bool res = false;
	//A series of aproiri test
	if (strlen(dt) != 29)
		return res;
	if (strcmp("GMT", &dt[26]) != 0)
		return res;
	if (dt[3] != ',')
		return res;
	if ((dt[19] != ':') || (dt[22] != ':'))
		return res;
	int digits[] = { 5, 6, 12, 13, 14, 15, 17, 18, 20, 21, 23, 24 };
	BOOL rd;
	for (int i = 0; i < 12; i++)
	{
		char ch = dt[digits[i]];
		rd = (BOOL) isdigit(ch);
		if (!rd)
			return res;
	}

	//Check day
	char fmtDOM[] = "%3s";
	char dayStr[4];
	sscanf_s(dt, fmtDOM, dayStr, 4);

	int i = 0;
	while ((dayStr[i] != 0) && (i < 3))
	{
		dayStr[i] = tolower(dayStr[i]);
		i++;
	}
	for (i = 0; i < 7; i++)
	{
		if (strcmp(days[i], dayStr) == 0)
		{
			res = true;
			break;
		}
	}
	if (!res)
		return res;

	//Check month
	res = false;

	char * fmtMonth = "%3s";
	char monthStr[4];


	sscanf_s(&dt[startOfMonStr], fmtMonth, monthStr, 4);
	i = 0;

	while ((monthStr[i] != 0) && (i < 3))
	{
		monthStr[i] = tolower(monthStr[i]);
		i++;
	}
	for (i = 0; i < 12; i++)
	{
		if (strcmp(months[i], monthStr) == 0)
		{
			res = true;
			break;
		}
	}

	return res;

}




time_t ParseDateTime(char * dt, tm * dTime)
{
	char fmt[] = "DAY, %d MON %d %d:%d:%d GMT";
	char fmtDOM[] = "%3s";
	char fmtMonth[] = "%3s";

	int dtVal[10];
	char dayStr[4];
	char monthStr[4];

	//Extract the DOW
	sscanf_s(dt, fmtDOM, dayStr, 4);
	int i;
	int dow;



	//First match the day string in fmt to the DOW
	fmt[0] = dayStr[0];
	fmt[1] = dayStr[1];
	fmt[2] = dayStr[2];


	i = 0;
	while ((dayStr[i] != 0) && (i < 3))
	{
		dayStr[i] = tolower(dayStr[i]);
		i++;
	}
	dow = 0;
	for (i = 0; i < 7; i++)
	{
		if (strcmp(days[i], dayStr) == 0)
			break;
		dow++; //I never trust the for loop counter final value being available after the loop
	}

	//If not recognised then dow will be 8 and could generate error later!


	//Get the month
	sscanf_s(&dt[startOfMonStr], fmtMonth, monthStr, 4);

	//Adjust the fmt string for the actual month
	fmt[8] = monthStr[0];
	fmt[9] = monthStr[1];
	fmt[10] = monthStr[2];

	i = 0;
	while ((monthStr[i] != 0) && (i < 3))
	{
		monthStr[i] = tolower(monthStr[i]);
		i++;
	}
	int iMonth = 1;
	for (i = 0; i < 12; i++)
	{
		if (strcmp(months[i], monthStr) == 0)
			break;
		iMonth++; //I never trust the for loop counter final value being available after the loop
	}
	//If not recognised then month will be 13 and will generate error later!
	dtVal[1] = iMonth;

	//	//Get most of the dateTime integers (Except month (dtVal[1])
	sscanf_s(dt, fmt, &dtVal[0], &dtVal[2], &dtVal[3], &dtVal[4], &dtVal[5]);

	//char * dt = "Sun, 12 Apr 2015 11:46:17 GMT";

	dTime->tm_mon = dtVal[1] - 1;
	dTime->tm_year = dtVal[2] - 1900;
	dTime->tm_mon = dtVal[1];
	dTime->tm_mday = dtVal[0];
	dTime->tm_hour = dtVal[3];
	dTime->tm_min = dtVal[4];
	dTime->tm_sec = dtVal[5];
	dTime->tm_wday = dow;


	//ToD: add wday and yday values.
	dTime->tm_isdst = false; //We are processing GMT/UMT*/

	time_t  tt = _mktime64(dTime);
	return tt;


}

